<template>
	<div>
		<div>
			<i class="el-icon-document"></i>
			<span>平台列表</span>
			<el-button style="margin-left: 20px" @click="edit_btn()" size="mini" type="primary">新增</el-button>
		</div>
		<el-table :data="tableData" stripe style="width: 100%">

			<el-table-column prop="name" label="平台名称">
			</el-table-column>
			<el-table-column align="center" label="操作" width="200">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" @click="edit_btn(scope.row)">编辑</el-button>
					<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 商品编辑栏 -->
		<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="dialogVisible" width="70%"
			:append-to-body="true" :close-on-click-modal="false" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="130px" :rules="rules">
				<el-form-item label="平台名称">
					<el-input v-model="form.name" style="width: 100%" :placeholder="'请输入平台名称'"></el-input>
				</el-form-item>

			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" :disabled="isDisable" @click="submit('form')">确 定</el-button>
			</span>
		</el-dialog>
	</div>
	</div>
</template>

<script>
	import axios from "axios";

	export default {
		data() {
			return {
				tableData: [],
				listQuery: {},
				form: {},
				isDisable: false,
				dialogVisible: false,
				rules: {
					name: [{
						required: true,
						message: '请输入平台名称',
						trigger: 'blur'
					}, ],

				},
			}
		},
		created() {
			this.initPage();
		},
		methods: {
			initPage(data) {
				let that = this;
				axios.get("Platform/index", ).then(function(response) {
					let res = response.data;
					if (res.code === 1) {
						that.tableData = res.data.list;
					} else {
						that.$Message.error(res.msg);
					}
				});
			},
			handleSearchList() {
				this.initPage(this.listQuery);
			},
			handleResetSearch() {
				this.listQuery = {};
				this.initPage();
			},
			edit_btn(row) {
				if (row) {
					this.form = row;
				} else {
					this.form = {};
				}

				this.dialogVisible = true;
			},
			handleClose() {
				this.form = {};
				this.dialogVisible = false;
				this.$refs["form"].resetFields();
			},
			handleDelete(row) {
				let vm = this;
				this.$confirm("确定要删除么?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
				}).then(function(response) {
					axios
						.post("AbnormalConfig/del", {
							id: row.id,
						})
						.then((res) => {
							if (res.data.code === 1) {
								vm.$Message.success(res.data.msg);
								//初始化
								vm.initPage();
							} else {
								vm.$Message.error(res.data.msg);
							}
						});
				});
			},
			submit(formName) {
				this.isDisable = true;
				console.log(this.form)
				var url = 'Platform/add';
				if(this.form.id){
					url = 'Platform/edit';
				}
				this.$refs[formName].validate((valid) => {
					if (valid) {
						axios.post(url, this.form).then((response) => {
							setTimeout(() => {
								this.isDisable = false;
							}, 1000);
							if (response.data.code === 1) {
								this.$Message.success(response.data.msg);
								this.handleClose();
								this.initPage();
							} else {
								this.$message({
									message: response.data.msg,
									type: "warning",
								});
							}
						});
					} else {
						setTimeout(() => {
							this.isDisable = false;
						}, 1000);
						return false;
					}
				});
			},
		}
	}
</script>
